/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
 */
package carstore.userserver.controllers;

import carstore.userserver.models.Car;
import carstore.userserver.models.UserInfo;
import carstore.userserver.services.UserInfoRepository;
import carstore.userserver.services.UserRepository;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * @author wuhao
 */
@RestController
public class UserInfoController {

        @Autowired
        private UserInfoRepository UserInfoRepositoryImpl;

        @RequestMapping(value = "/setselectstatus")
        public void setCarSelectStatus(Integer UserId, Integer CarId) {
                //System.out.println("userserver.userInfoController.setCarSelectStatus");
                List<UserInfo> UserInfoList = UserInfoRepositoryImpl.findByUserId(UserId);
                if (UserInfoList.size() != 0) {
                        for (int i = 0; i < UserInfoList.size(); i++) {
                                UserInfo userInfo = UserInfoList.get(i);
                                userInfo.setSelected(userInfo.getId() == CarId);

                                UserInfoRepositoryImpl.save(userInfo);
                        }
                }
        }

        @RequestMapping("/carsbyuser")
        public List<Car> getUsercars(Integer UserId) {
                //System.out.println("userinfocontroller.getUsercars");
                List<Car> Carlist = new ArrayList<Car>();
//		Car car=new Car();
//		car.setId(1);
//		car.setCarNAME("富康");
//		car.setCarSIZE("2.0");
//		car.setFactoryId("神龙");
//		Carlist.add(car);
//
//		car=new Car();
//		car.setId(2);
//		car.setCarNAME("轩逸");
//		car.setCarSIZE("3.0");
//		car.setFactoryId("日产");
//		Carlist.add(car);

//		return Carlist;
//return UserInfoRepositoryImpl.findCarsByUserId(UserId);
                int tmpUserId = 1;
                List<Object[]> SrcCarlist = UserInfoRepositoryImpl.findCarsByUserId(tmpUserId);
                for (int i = 0; i < SrcCarlist.size(); i++) {
                        Object[] obj = SrcCarlist.get(i);
                        Car car = new Car();
                        //car.setId(i);
//System.out.println(obj[0].toString());

                        car.setId(Integer.parseInt(obj[0].toString()));
                        car.setFactoryId(obj[1].toString());
                        car.setCarNAME(obj[2].toString());
                        car.setCarSIZE(obj[3].toString());
                        car.setSelected((Boolean) obj[4]);
                        Carlist.add(car);
//System.out.println(car.getId()+": "+car.getSelected());
                }
//System.out.println(Carlist.get(0).getCarNAME());
                return Carlist;
        }
}
